<template>
  <a-modal
    title="出库单审核"
    :width="600"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="取消"
    okText="确定"
  >
    <div v-if="model.id">
      <!-- 出库单基本信息 -->
      <a-descriptions title="出库单信息" :column="2" size="small" bordered>
        <a-descriptions-item label="出库单号">{{ model.outboundNo }}</a-descriptions-item>
        <a-descriptions-item label="客户名称">{{ model.customerName }}</a-descriptions-item>
        <a-descriptions-item label="出库总数量">{{ model.totalQuantity }}</a-descriptions-item>
      </a-descriptions>

      <a-divider />

      <!-- 审核操作 -->
      <a-form-model
        ref="form"
        :model="auditForm"
        :rules="validatorRules"
        :label-col="{ span: 4 }"
        :wrapper-col="{ span: 20 }"
      >
        <a-form-model-item label="审核结果" prop="auditResult">
          <a-radio-group v-model="auditForm.auditResult">
            <a-radio :value="1">审核通过</a-radio>
            <a-radio :value="2">审核驳回</a-radio>
          </a-radio-group>
        </a-form-model-item>

        <a-form-model-item label="审核意见" prop="auditRemark">
          <a-textarea
            v-model="auditForm.auditRemark"
            placeholder="请输入审核意见"
            :rows="4"
            :maxLength="500"
            showCount
          />
        </a-form-model-item>
      </a-form-model>
    </div>
  </a-modal>
</template>

<script>
import { httpAction } from '@/api/manage'

export default {
  name: 'OutboundAuditModal',
  data() {
    return {
      visible: false,
      confirmLoading: false,
      model: {},
      auditForm: {
        auditResult: 1,
        auditRemark: '',
      },
      validatorRules: {
        auditResult: [{ required: true, message: '请选择审核结果!' }],
        auditRemark: [{ required: true, message: '请输入审核意见!' }],
      },
    }
  },
  methods: {
    show(record) {
      this.visible = true
      this.model = { ...record }
      this.auditForm = {
        auditResult: 1,
        auditRemark: '',
      }
    },
    close() {
      this.visible = false
      this.model = {}
      this.auditForm = {
        auditResult: 1,
        auditRemark: '',
      }
      this.$refs.form && this.$refs.form.resetFields()
    },
    handleOk() {
      this.$refs.form.validate(async (valid) => {
        console.log(valid)
        if (!valid) return

        this.confirmLoading = true
        try {
          const params = new URLSearchParams()
          params.append('auditResult', this.auditForm.auditResult)
          params.append('auditRemark', this.auditForm.auditRemark)

          console.log('................................', params.toString())
          const result = await httpAction(`/wms/finished-goods/outbound/audit/${this.model.id}?${params.toString()}`, null, 'post')
          const resultText = this.auditForm.auditResult === 1 ? '审核通过' : '审核驳回'
          this.$message.success(`出库单${resultText}成功！`)
          this.$emit('ok')
          this.close()
        } catch (error) {
          this.$message.error('审核失败：' + error.message)
        } finally {
          this.confirmLoading = false
        }
      })
    },
    handleCancel() {
      this.close()
    },
    getOutboundTypeText(type) {
      const typeMap = { 1: '销售出库', 2: '调拨出库', 3: '其他出库' }
      return typeMap[type] || '未知'
    },
    getOutboundTypeColor(type) {
      const colorMap = { 1: 'blue', 2: 'orange', 3: 'purple' }
      return colorMap[type] || 'default'
    },
  },
}
</script>
